package pro.shuangxi.web;

import org.openqa.selenium.chrome.ChromeDriver;
import ru.yandex.qatools.ashot.AShot;
import ru.yandex.qatools.ashot.Screenshot;
import ru.yandex.qatools.ashot.shooting.ShootingStrategies;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;


/**
 * @author mengshx
 * @description <p>浏览器截图，需要容器初始化完毕</p>
 * @date 2022-08-25 15:16
 **/
public class WebScreenUtils {

    public static BufferedImage getImage(String url)  {
        ChromeDriver singleWebDriver = WebBrowserUtils.getSingleWebDriver();
        singleWebDriver.get(url);
        Screenshot screenshot = new AShot()
                .shootingStrategy(ShootingStrategies.viewportPasting(400))
                .takeScreenshot(singleWebDriver);
        BufferedImage image = screenshot.getImage();
        return image;
    }
    public static byte[] getImageBytes(String url)  {
        BufferedImage image = getImage(url);
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        try {
            ImageIO.write(image, "png",outputStream);
            outputStream.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return outputStream.toByteArray();
    }



}
